
//
//  TBPackagesModel.swift
//  新浪微博
//
//  Created by bin on 16/2/4.
//  Copyright © 2016年 XMG. All rights reserved.
//

import UIKit

let line = 7

class TBPackagesModel: NSObject {

    var group_name_cn: String?
    var emoticons: [TBEmoticonModel]?
    var documentPath: String?
    
    class func loadAllPackage() -> [TBPackagesModel]{
        
        //emoticons.plist的路径
        let emoticonsPath = NSBundle.mainBundle().pathForResource("emoticons.plist", ofType: nil, inDirectory: "Emoticons.bundle")
        let emoticonsDict = NSDictionary(contentsOfFile: emoticonsPath!)
        // id ：文件名。数组
        let itemArr = emoticonsDict!["packages"] as! [[String: AnyObject]]
        // xxxx/Emoticons.bundle
        let bundlePath = (emoticonsPath! as NSString).stringByDeletingLastPathComponent
        var packagesModelArr = [TBPackagesModel]()
        //最近package
        let package = TBPackagesModel()
        var emoticons = [TBEmoticonModel]()
        for _ in 0..<21{
        
            emoticons.append(TBEmoticonModel(dict: nil, documentPath: nil, isRemoveBtn: false))
        }
        package.emoticons = emoticons
        packagesModelArr.append(package)
       
        for item in itemArr{
            let packagePath = (bundlePath as NSString).stringByAppendingPathComponent((item["id"] as! String))
            let infoPath = (packagePath as NSString).stringByAppendingPathComponent("info.plist")
            let dict = NSDictionary(contentsOfFile: infoPath) as! [String : AnyObject]
            let packagesModel = TBPackagesModel(dict: dict,documentPath:packagePath)
            packagesModel.documentPath = packagePath
            packagesModelArr.append(packagesModel)
            
        }
        return packagesModelArr
    }
    
  
    override init(){
        super.init()
    }
    
    init(dict: [String: AnyObject], documentPath: String){
        super.init()
        self.documentPath = documentPath
        setValuesForKeysWithDictionary(dict)
    }
    
    override func setValue(value: AnyObject?, forUndefinedKey key: String) {
        
    }
    // 只要重写这个属性, 就相当于OC中重写description方法
    override var description: String {
        // 优化
        let property = ["name", "age"]
        let dict =  dictionaryWithValuesForKeys(property)
        return "\(dict)"
    }
    // （模型中属性为模型）KVC的setValuesForKeysWithDictionary方法内部会调用下面这个方法
    override func setValue(value: AnyObject?, forKey key: String) {
        if key == "emoticons"
        {
            let dictArr = value as! [[String: AnyObject]]
            var models = [TBEmoticonModel]()
            var count = 1
            for dict in dictArr{
                let emoticon = (count % 21 == 0) ?  TBEmoticonModel(dict: nil, documentPath:nil, isRemoveBtn: true) :
                TBEmoticonModel(dict: dict, documentPath:documentPath!, isRemoveBtn: false)
                models.append(emoticon)
                count++
            }
            let rowCount = (dictArr.count - 1) / line + 1
            for _ in (dictArr.count + 1)...(rowCount * line) {
                
                let emoticon = TBEmoticonModel(dict: nil, documentPath: nil, isRemoveBtn: false)
                models.append(emoticon)
            }
            models.removeLast()
            models.append(TBEmoticonModel(dict: nil, documentPath:nil, isRemoveBtn: true))

            emoticons = models
            return 
        }
        super.setValue(value, forKey: key)
    }
}

class TBEmoticonModel: NSObject {
    
    var chs: String?
    var cht: String?
    var gif: String?
    var png: String?{
        
        didSet{
            pngPath = (documentPath! as NSString).stringByAppendingPathComponent(png!)
        }
    }
    var code: String?{
        didSet{
            // 1.创建一个扫描器
            let scanner = NSScanner(string: code ?? "")
            // 2.从字符串中扫描出对应的十六进制
            var res: UInt32 = 0
            scanner.scanHexInt(&res)
            // 3.根据十六进制创建一个新的表情字符串
            codeStr = "\(Character(UnicodeScalar(res)))"
        }
    }
    
    var documentPath: String?
    var pngPath: String?
    var codeStr: String?
    var isRemoveBtn = false
    
    //MARK: - 字典转模型
    init(dict: [String: AnyObject]?, documentPath: String?, isRemoveBtn: Bool){
        super.init()
        self.documentPath = documentPath
        self.isRemoveBtn = isRemoveBtn
        if let d = dict{
            setValuesForKeysWithDictionary(d)
        }
    }
    
    override func setValue(value: AnyObject?, forUndefinedKey key: String) {
        
    }
    
    // 只要重写这个属性, 就相当于OC中重写description方法
    override var description: String {
        // 优化
        let property = ["name", "age"]
        let dict =  dictionaryWithValuesForKeys(property)
        return "\(dict)"
    }
    

}
